<html>
<head><meta charset="utf-8"><title>macro by example allows attributes on expressions? · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/macro.20by.20example.20allows.20attributes.20on.20expressions.3F.html">macro by example allows attributes on expressions?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="203857748"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/macro%20by%20example%20allows%20attributes%20on%20expressions%3F/near/203857748" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/macro.20by.20example.20allows.20attributes.20on.20expressions.3F.html#203857748">(Jul 14 2020 at 16:52)</a>:</h4>
<p>Today I came up with this example, which seems very strange to me</p>



<a name="203857756"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/macro%20by%20example%20allows%20attributes%20on%20expressions%3F/near/203857756" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/macro.20by.20example.20allows.20attributes.20on.20expressions.3F.html#203857756">(Jul 14 2020 at 16:52)</a>:</h4>
<p><a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=dbe121888e0bb85752f0c5c4bfc694b1">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=dbe121888e0bb85752f0c5c4bfc694b1</a></p>



<a name="203857814"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/macro%20by%20example%20allows%20attributes%20on%20expressions%3F/near/203857814" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/macro.20by.20example.20allows.20attributes.20on.20expressions.3F.html#203857814">(Jul 14 2020 at 16:52)</a>:</h4>
<div class="codehilite"><pre><span></span><code><span class="c1">// This doesn&#39;t work</span>
<span class="k">static</span><span class="w"> </span><span class="n">B</span>: <span class="p">()</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="cp">#[allow(clippy::trivial_regex)]</span><span class="w"> </span><span class="p">();</span><span class="w"></span>

<span class="c1">// This works O_o</span>
<span class="n">lazy_static</span>::<span class="n">lazy_static</span><span class="o">!</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">static</span><span class="w"> </span><span class="k">ref</span><span class="w"> </span><span class="n">A</span>: <span class="p">()</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="cp">#[allow(clippy::trivial_regex)]</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="203857839"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/macro%20by%20example%20allows%20attributes%20on%20expressions%3F/near/203857839" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/macro.20by.20example.20allows.20attributes.20on.20expressions.3F.html#203857839">(Jul 14 2020 at 16:52)</a>:</h4>
<p>Is this intended behavior?</p>



<a name="203858309"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/macro%20by%20example%20allows%20attributes%20on%20expressions%3F/near/203858309" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/macro.20by.20example.20allows.20attributes.20on.20expressions.3F.html#203858309">(Jul 14 2020 at 16:56)</a>:</h4>
<p>Hm, seems like this is actually unrelated to MBE, minimized example:</p>
<div class="codehilite"><pre><span></span><code><span class="k">fn</span> <span class="nf">works</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">   </span><span class="cp">#[allow(clippy::trivial_regex)]</span><span class="w"> </span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">doesnt</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">   </span><span class="cp">#[rustfmt::skip]</span><span class="w"> </span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="203858550"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/macro%20by%20example%20allows%20attributes%20on%20expressions%3F/near/203858550" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/macro.20by.20example.20allows.20attributes.20on.20expressions.3F.html#203858550">(Jul 14 2020 at 16:58)</a>:</h4>
<p>Seen in the wild here: <a href="https://github.com/getsentry/relay/blob/3f2d0c96a8ffe427f0c45f1960674ffae5890b88/relay-general/src/pii/processor.rs#L21-L24">https://github.com/getsentry/relay/blob/3f2d0c96a8ffe427f0c45f1960674ffae5890b88/relay-general/src/pii/processor.rs#L21-L24</a></p>



<a name="203863995"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/macro%20by%20example%20allows%20attributes%20on%20expressions%3F/near/203863995" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eric Huss <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/macro.20by.20example.20allows.20attributes.20on.20expressions.3F.html#203863995">(Jul 14 2020 at 17:42)</a>:</h4>
<p><span class="user-mention" data-user-id="133169">@matklad</span> attributes on expressions were accidentally stabilized in a few cases (detailed at <a href="https://doc.rust-lang.org/nightly/reference/expressions.html#expression-attributes">https://doc.rust-lang.org/nightly/reference/expressions.html#expression-attributes</a>).  It looks like tool-attributes on expressions weren't stabilized, which is very curious!  I had not seen that difference before.  Looks like the restriction also applies to proc-macros.  Strange!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>